﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

using VerbrauchProcessor.DatabaseAccess.DataModel;
using VerbrauchProcessor.DatabaseAccess.Reader;
using VerbrauchProcessor.Export;

namespace VerbrauchProcessor.source.GUI
{
	public partial class MainWindow : Form
	{

		#region Fields
		
		private string mDatabasePath; 
		
		#endregion


		#region Constructors
		
		public MainWindow()
		{
			InitializeComponent();
		} 
		
		#endregion

		#region Event Handlers
		
		private void onOpenFileButton_Clicked(object sender, EventArgs e)
		{
			mOpenFileDialog.CheckFileExists = true;
			mOpenFileDialog.Filter = "Access Database |*.mdb";

			if (mOpenFileDialog.ShowDialog() == DialogResult.OK)
			{
				mDatabasePath = mOpenFileDialog.FileName;
				mTextboxPathDatabase.Text = mOpenFileDialog.FileName;
			}

			// enable calculations
			mButtonGo.Enabled = true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void onButtonGo_Clicked(object sender, EventArgs e)
		{
			if (String.IsNullOrWhiteSpace(mDatabasePath) == false)
			{
				// read access file
				MDBReader mdbReader = new MDBReader();
				IList<Sample> samples = mdbReader.Read(mDatabasePath);
				IList<MeasurementIntervall> measurmentIntervalls = null;

				// get stats for one year
				if (!String.IsNullOrWhiteSpace(mYearTextInput.Text) &&
					(mCheckBoxAll.Checked == false))
				{
					// get stats
					measurmentIntervalls = Stats.GetMeasurementIntervallsForYear(samples, mYearTextInput.Text);

				}// get stats over all years
				else if (mCheckBoxAll.Checked == true)
				{
					// get stats
					measurmentIntervalls = Stats.GetMeasurementIntervalls(samples);
				}
				else
				{
					MessageBox.Show("Ungültige Eingabe");
				}

				if (measurmentIntervalls != null)
				{
					new ExcelExport().ExportData(measurmentIntervalls);
				}
			}
			else
			{
				MessageBox.Show("Ungültige Datenbank");
			}
		} 
		#endregion
	}
}
